﻿using csharp_books_415.Models;
using csharp_books_415.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using csharp_books_415.Adapters;


namespace csharp_books_415.Controllers
{
    public class HomeController : Controller
    {

        // define adapter
        IBooksAdapter _adapter;

        // constructor
        public HomeController()
        {
            //instanciate adapter
            _adapter = new BookDataAdapter();
        }
        // overload constructor
        public HomeController(IBooksAdapter adp)
        {
            _adapter = adp;
        }

        // READ
        public ActionResult Index()
        {
            // books VM
            BooksIndexVm iVm = new BooksIndexVm();
            iVm.Greeting = "Bonjour Montreal";
            iVm.Books = _adapter.GetBooks();  // data
            return View(iVm);     // view
        }
        public ActionResult Details(int bid)
        {
            // vm
            BooksDetailsVm dVm = new BooksDetailsVm();
            dVm.Greeting = "Details for books....";
            // fetch data from Adapter???
            dVm.BookDetails = _adapter.GetBook(bid);
            var x = 100;
            return View(dVm); // send VM to view
        }

        // CREATE
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(BookCreateVm cVm)
        {
            _adapter.AddBook(cVm);
            return RedirectToAction("Index");
        }


        // DELETE
        public ActionResult Delete(int bid)
        {
            _adapter.DeleteBook(bid);
            return RedirectToAction("Index");
        }

        // EDIT / UPDATE
        // show page, wait to be update
        public ActionResult Edit(int bid)
        {
            // VM
            BooksEditVm eVm = new BooksEditVm();
            // populate VM
            eVm.Greeting = "bonjour missure";
            eVm.Book = _adapter.GetBook(bid);
            // Send to view...
            return View(eVm);
        }
        [HttpPost]
        public ActionResult Edit(BooksEditVm vm)
        {
            _adapter.SaveBook(vm);
            return RedirectToAction("Index");
        }



        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}